package org.zsk.cs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zsk.cs.domain.po.Notice;
import org.zsk.cs.domain.vo.IdentityAuthVo;
import org.zsk.cs.domain.vo.NoticeVo;
import org.zsk.cs.domain.vo.NoticeVo;
import org.zsk.cs.response.ResultBody;
import org.zsk.cs.service.NoticeService;
import org.zsk.fastdev.assign.Assign;

import java.util.List;

/**
 * <p>
 * 公告 controller
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/29
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    private final NoticeService noticeService;
    private final Assign assign;

    @Autowired
    public NoticeController(NoticeService noticeService, Assign assign) {
        this.noticeService = noticeService;
        this.assign = assign;
    }

    @PostMapping("/a")
    public ResultBody add(@RequestBody NoticeVo noticeVo) throws Exception {
        Notice notice=new Notice();
        assign.assign(noticeVo,notice,"$districtHeadId,$content");
        noticeService.save(notice);
        return ResultBody.success();
    }

    @PutMapping("/u")
    public ResultBody update(@RequestBody NoticeVo noticeVo) throws Exception {
        Notice notice=new Notice();
        assign.assign(noticeVo,notice);
        noticeService.updateById(notice);
        return ResultBody.success();
    }

    @DeleteMapping("/d/{ids}")
    public ResultBody delete(@PathVariable List<Integer> ids) throws Exception {
        noticeService.removeByIds(ids);
        return ResultBody.success();
    }

    @GetMapping("/g")
    public ResultBody page(NoticeVo noticeVo) throws Exception {
        Page<Notice> page=new Page<>();
        assign.assign(noticeVo,page,"$current,$size");
        return ResultBody.success(noticeService.page(page));
    }

    @GetMapping("/getWithUserNameByPage")
    public ResultBody getWithUserNameByPage(NoticeVo noticeVo) throws Exception {
        return ResultBody.success(noticeService.getWithUserNameByPage(noticeVo));
    }

    @GetMapping("/districtHeadGetWithUserNameByPage")
    public ResultBody districtHeadGetWithUserNameByPage(NoticeVo noticeVo) throws Exception {
        return ResultBody.success(noticeService.districtHeadGetWithUserNameByPage(noticeVo));
    }

    @PostMapping("/districtHeadSave")
    public ResultBody districtHeadSave(@RequestBody NoticeVo noticeVo) throws Exception {
        noticeService.districtHeadSave(noticeVo);
        return ResultBody.success();
    }

    @GetMapping("/residentGetWithUserNameByPage")
    public ResultBody residentGetWithUserNameByPage(NoticeVo noticeVo) throws Exception {
        return ResultBody.success(noticeService.residentGetWithUserNameByPage(noticeVo));
    }
}
